<?php

/*
 * This file is part of the sfSAMLplugin package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 * (c) 2010      Théophile Helleboid <t.helleboid@iariss.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfSAMLPlugin configuration.
 * 
 * @package    sfSAMLPlugin
 * @subpackage sfSAMLAuth
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @author     Théophile Helleboid <t.helleboid@iariss.fr>
 */
class BasesfSAMLAuthActions extends sfActions
{
  public function executeSignin($request)
  {
    $user = $this->getUser();
 


    // Create SimpleSAML module
    $simpleSAMLAuth = new SimpleSAML_Auth_Simple('default-sp');
    //Obtengo las variables de la sesion 

    $attributes = $simpleSAMLAuth->getAttributes();


    // If the user is authenticated from the IdP
    if ($simpleSAMLAuth->isAuthenticated())
    {      

            //verifico si esta asociado en la base de datos de comande 
            $email_user_practil = $attributes['id'][0];
            $new_criteria = new Criteria();
            $new_criteria->add(UsuarioPeer::ID_PRACTIL, $email_user_practil);
            $objeto_user_local = UsuarioPeer::doSelectOne($new_criteria);

            if(is_object($objeto_user_local))
            { //--> Si el usuario existe en la base de datos local 

                //cargo las sesiones locales para comande
                //pongo en sesion el id del usuario pero de practil
                $this->getUser()->setAttribute('ses_user_id_practil',$attributes['id'][0]);

                //sesiones locales
                $this->getUser()->setAttribute('ses_user_id',$objeto_user_local->getId());
                $this->getUser()->setAttribute('ses_user_nombre',$objeto_user_local->getNombres());
                $this->getUser()->setAttribute('ses_user_ape',$objeto_user_local->getApellidos());
                $this->getUser()->setAttribute('ses_user_correo',$objeto_user_local->getEmail());


                $this->getUser()->setAttribute('ses_currentUserObject',$objeto_user_local);
                $this->redirect('monitoreo/index');

            }
            else
            { //---> mando a la pagina donde pregunto al usuario si desea asociar su cuenta con la plataforma comande
                $this->redirect('confirmacion/asociarcuenta');
            }
       
      


    }
    else  // the user is not authenticated in symfony and from the IdP
    {
     
            $this->url_idp = $simpleSAMLAuth->login();
  
    }
  }

  public function executeSignout($request)
  {
    
    $signoutUrl = sfConfig::get('app_sf_guard_plugin_success_signout_url');

    $simpleSAMLAuth = new SimpleSAML_Auth_Simple('default-sp');
    $simpleSAMLAuth->logout($this->generateUrl('' != $signoutUrl ? $signoutUrl : 'homepage'), array(), true);
    
    // Nothing happen after there
    $this->redirect('' != $signoutUrl ? $signoutUrl : 'homepage');

  }




  public function executeSecure($request)
  {
    $this->getResponse()->setStatusCode(403);
  }

  public function executePassword($request)
  {
    throw new sfException('This method is not yet implemented.');
  }



}
